package cn.edu.njuit.server.ugcnote.ugcnoteserver.service.impl;

import cn.edu.njuit.server.ugcnote.ugcnoteserver.entity.NoteInfo;
import cn.edu.njuit.server.ugcnote.ugcnoteserver.repository.NoteInfoRepository;
import cn.edu.njuit.server.ugcnote.ugcnoteserver.service.NoteInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Optional;

@Service
public class NoteInfoServiceImpl implements NoteInfoService {
    @Autowired
    NoteInfoRepository noteInfoRepository;
    @Override
    public Page<NoteInfo> findAllNotes(Long typeId, Integer page, Integer size) {
        return this.noteInfoRepository.findPublishedNotes(typeId, PageRequest.of(page,size));
    }

    @Override
    public NoteInfo publishNote(NoteInfo note) {
        note.setPublishTime(LocalDateTime.now());
        note.setNoteStatus("1");
        return this.noteInfoRepository.save(note);
    }

    @Override
    public NoteInfo getNoteInfo(Long id) {
        Optional<NoteInfo> noteOptional=this.noteInfoRepository.findById(id);
        if(noteOptional.isPresent()){
            return noteOptional.get();
        }
        return null;
    }
}
